<template>
  <div class="ident-audit">
    <!-- 搜索栏 -->
    <div class="search-block">
      <div class="item-msg">
        <label>状态</label>
        <el-select v-model="searchForm.status" placeholder="全部" size="small" @change="search()">
          <el-option value label="全部"></el-option>
          <el-option value="2" label="待审核"></el-option>
          <el-option value="-2" label="已拒绝"></el-option>
          <el-option value="1" label="已通过"></el-option>
        </el-select>
      </div>
      <div class="item-msg combain">
        <label>精确查找</label>
        <el-input
          placeholder="UID/Name/Phone/Email"
          v-model="searchForm.search"
          class="input-with-select"
          size="small"
          clearable
        >
          <el-button slot="append" icon="el-icon-search" @click="search()"></el-button>
        </el-input>
      </div>
      <div class="item-msg">
        <label>提交时间</label>
        <el-date-picker
          size="small"
          v-model="searchForm.times"
          type="daterange"
          value-format="timestamp"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
      <el-button class="search-btn" @click="initSearch()" size="small" type="primary">重置筛选</el-button>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        size="small"
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column align="center" prop="uid" label="UID" width="80"></el-table-column>
        <el-table-column align="center" prop="inviter" label="推荐人ID" width="80">
          <template slot-scope="item">{{item.row.inviter===0?'/':item.row.inviter}}</template>
        </el-table-column>
        <el-table-column align="center" label="姓名" width="120">
          <template slot-scope="item">{{item.row.name||'/'}}</template>
        </el-table-column>
        <el-table-column align="center" prop="country" label="国籍"></el-table-column>
        <el-table-column align="center" prop="mobile" label="手机"></el-table-column>
        <el-table-column align="center" prop="email" label="邮箱"></el-table-column>
        <el-table-column align="center" prop="card_type" label="证件类型" width="170"></el-table-column>
        <el-table-column align="center" prop="card_id" label="证件号" width="180"></el-table-column>
        <el-table-column align="center" label="认证文件" width="100">
          <template slot-scope="item">
            <el-button type="text" @click="viewInfo(item.row)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="updated_at" width="175" label="提交时间"></el-table-column>
        <el-table-column align="center" prop="updated_at" label="审核状态">
          <template slot-scope="item">{{item.row.status|auditStatus}}</template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
          width="120"
          v-if="['','2','1'].includes(searchForm.status)"
        >
          <template slot-scope="item">
            <el-button
              type="text"
              class="operate-btn allow"
              size="small"
              @click="audit(item.row.uid,'verify')"
              v-if="item.row.status==='2'"
            >通过</el-button>
            <el-button
              type="text"
              class="operate-btn reject"
              size="small"
              @click="audit(item.row.uid,'refuse')"
              v-if="item.row.status==='2'"
            >拒绝</el-button>
            <el-button
              type="text"
              class="operate-btn cancel"
              size="small"
              @click="audit(item.row.uid,'revoke')"
              v-if="item.row.status==='1'"
            >撤销</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pages
        :pages="searchForm"
        :total="total"
        @pageSizeChange="pageSizeChange"
        @pageChange="pageChange"
      ></pages>
      <!-- 查看资产弹窗 -->
      <transition name="fade">
        <audit-dialog v-if="showVideo" @cancel="showVideo=false" :info="info"></audit-dialog>
      </transition>
    </div>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import AuditDialog from "@/components/UserList/AuditDialog.vue";
import { userAuditList, userAudit } from "./User.service";
export default {
  components: {
    Pages,
    AuditDialog
  },
  computed: {},
  data() {
    return {
      loading: false,
      searchForm: {
        status: "",
        search: "",
        start_time: "",
        end_time: "",
        times: null,
        page: 1,
        size: 10
      },
      tableData: [],
      total: 10,
      showVideo: false,
      info: {}
    };
  },
  watch: {
    "searchForm.times": function(time) {
      if (time) {
        this.searchForm.start_time = time[0] / 1000;
        this.searchForm.end_time = time[1] / 1000;
      } else {
        this.searchForm.start_time = this.searchForm.end_time = "";
      }
      this.search();
    }
  },
  mounted() {
    this.initSearch();
  },
  methods: {
    initSearch() {
      this.searchForm = {
        status: "",
        search: "",
        start_time: "",
        end_time: "",
        times: null,
        page: 1,
        size: 10
      };
      this.search();
    },
    async search() {
      this.loading = true;
      const res = await userAuditList(this.searchForm);
      this.tableData = res.data.data;
      this.total = Number(res.data.count);
      this.loading = false;
    },
    audit(uid, action) {
      let param = {
        uid,
        action
      };
      let info = "";
      switch (action) {
        case "verify":
          info = "通过";
          break;
        case "refuse":
          info = "拒绝";
          break;
        case "revoke":
          info = "撤销";
          break;
        default:
          break;
      }
      this.$confirm(`确定${info}此用户的请求？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          userAudit(param).then(res => {
            if (res.code === 1) {
              console.log(res);
              this.$notify.success({
                title: "提示",
                message: "操作成功"
              });
              this.search();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    viewInfo(info) {
      this.info = info;
      this.showVideo = true;
    },
    pageChange(val) {
      this.searchForm.page = val;
      this.search(this.searchForm);
    },
    pageSizeChange(val) {
      this.searchForm.size = val;
      this.search(this.searchForm);
    }
  }
};
</script>

<style lang="scss" scoped>
.ident-audit {
  position: relative;
  min-height: calc(100vh - 100px);
  .search-block {
    width: 100%;
    padding: 25px 30px;
    border-radius: 5px;
    background: #fff;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .item-msg {
      display: inline-block;
      margin-right: 20px;
      height: 60px;
      line-height: 60px;
      font-size: 1 size= "medium" 4;
      color: $admin_font_param;
      label {
        display: inline-block;
        max-width: 90px;
        text-align: right;
        margin-right: 10px;
      }
      .el-input {
        width: 150px;
      }
      .el-select {
        width: 150px;
      }
      .el-date-editor {
        width: 270px;
      }
    }
    .combain {
      .el-select {
        width: 80px;
      }
      .el-input {
        width: 260px;
      }
    }
    .search-btn {
      margin-left: 30px;
    }
  }
  .data-table {
    position: relative;
    padding-bottom: 20px;
    background: #fff;
    border-radius: 5px;
    margin-top: 30px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .reject {
      color: #f56c6c;
    }
    .cancel {
      color: #909399;
    }
  }
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
}
</style>